package com.cyf.service.impl;

import com.cyf.db.bean.AppCar;
import com.cyf.db.bean.Car;
import com.cyf.db.mapper.AppUserCarMapper;
import com.cyf.db.mapper.CarMapper;
import com.cyf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author hfy
 * @date 2018/7/13
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AppUserCarMapper appUserCarMapper;
    @Autowired
    private CarMapper carMapper;

    @Override
    public Map<Integer, Set<Integer>> getParkingPersonIdByUserId(Integer userId) {
        List<AppCar> appCars = appUserCarMapper.getCarByUserId(userId);

        // 车场id：人员id列表
        Map<Integer, Set<Integer>> parkingPersonIdList = new HashMap<>();
        if (appCars != null && appCars.size() > 0) {
            for (AppCar appCar : appCars) {
                List<Car> carList = carMapper.getByPlate(appCar.getPlate());
                if (carList != null) {
                    for (Car car : carList) {
                        Set<Integer> personIdSet = parkingPersonIdList.get(car.getParkingId());
                        if (personIdSet == null) {
                            personIdSet = new HashSet<>();
                        }
                        personIdSet.add(car.getPersonId());
                        parkingPersonIdList.put(car.getParkingId(), personIdSet);
                    }
                }
            }
        }
        return parkingPersonIdList;
    }

}
